$(function () {
    $("#demo")
        .jstree({ 
            "plugins" : [ "themes", "json_data", "ui", "crrm", "cookies", "dnd", "search", "types", "hotkeys", "contextmenu" ],
            "themes" : {
            	            "theme" : "default"
            	        },
            "json_data" : { 
                "ajax" : {
                    "url" : url("structure/operations"),
                    "data" : function (n) { 
                        return { 
                            "operation" : "get_children", 
                            "id" : n.attr ? n.attr("id").replace("node_","") : 1 
                        }; 
                    }
                }
            },
            "ui" : {
                "initially_select" : [ "node_4" ]
            },
            'types' : 
            {
            	"max_depth" : -2
            }
        })
        .bind("create.jstree", function (e, data) {

var parent_id = 0;
if (data.rslt.parent != -1)
{
//	alert('real level');
	parent_id = data.rslt.parent.attr("id");
}
else
{
//	alert('0 level');
}

            
            $.post(
                url("structure/operations"), 
                { 
                    "operation" : "create_node", 
                    "id" : parent_id, 
                    "position" : data.rslt.position,
                    "title" : data.rslt.name,
                    "type" : data.rslt.obj.attr("rel")
                }, 
                function (r) {
                    if(r.status) {
                        $(data.rslt.obj).attr("id", r.id);
                    }
                    else {
                        $.jstree.rollback(data.rlbk);
                    }
                    data.inst.refresh();
                }
            );
        })
        .bind("remove.jstree", function (e, data) {
            data.rslt.obj.each(function () {
                $.ajax({
                    async : false,
                    type: 'POST',
                    url: url("structure/operations"),
                    data : { 
                        "operation" : "remove_node", 
                        "id" : this.id.replace("node_","")
                    }, 
                    success : function (r) {
                        if(!r.status) {
                            data.inst.refresh();
                        }
                    }
                });
            });
        })
        .bind("rename.jstree", function (e, data) {
            $.post(
                url("structure/operations"), 
                { 
                    "operation" : "rename_node", 
                    "id" : data.rslt.obj.attr("id").replace("node_",""),
                    "title" : data.rslt.new_name
                }, 
                function (r) {
                    if(!r.status) {
                        $.jstree.rollback(data.rlbk);
                    }
                }
            );
        })
        .bind("move_node.jstree", function (e, data) {
            data.rslt.o.each(function (i) {
                $.ajax({
                    async : false,
                    type: 'POST',
                    url: url("structure/operations"),
                    data : { 
                        "operation" : "move_node", 
                        "id" : $(this).attr("id").replace("node_",""), 
                        "ref" : data.rslt.np.attr("id").replace("node_",""), 
                        "position" : data.rslt.cp + i,
                        "title" : data.rslt.name,
                        "copy" : data.rslt.cy ? 1 : 0
                    },
                    success : function (r) {
                        if(!r.status) {
                            $.jstree.rollback(data.rlbk);
                        }
                        else {
                            $(data.rslt.oc).attr("id", "node_" + r.id);
                            if(data.rslt.cy && $(data.rslt.oc).children("UL").length) {
                                data.inst.refresh(data.inst._get_parent(data.rslt.oc));
                            }
                        }
                        $("#analyze").click();
                    }
                });
            });
        });

    $("#demo").bind("select_node.jstree", function (event, data)
			 {
		  console.log(data.rslt.obj.attr("id"));  

		  $("#pmid").val(data.rslt.obj.attr("id"));
    	

   	 $("#linkDescription").html('Загрузка...');
$.getJSON('structure/get-link/id/'+data.rslt.obj.attr("id"), function (response)
		{
if (response.errorCode == 0)
{
    $("#linkDescription").html(response.data.Description);
}
else
{
//TODO:  KILL THIS!!!!!!!
	alert(response.errorDescription);
}
		});

    


		  });

    
});